﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports AugmentLib.InputOutput.Encryption

Partial Class MobileJob
    Inherits System.Web.UI.Page


#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        'LoadUpload()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        'LoadUpload()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadUpload()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

#End Region

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        Session("anchor1") = ""
        LoadUpload()
    End Sub

    Protected Sub btnResetSearchFilter_Click(sender As Object, e As EventArgs) Handles btnResetSearchFilter.Click
        CurrentPage = 0

        txtName.Text = String.Empty
        txtPhone.Text = String.Empty
        txtICNo.Text = String.Empty
        txtPosition.Text = String.Empty
        txtCreatedDateMin.Text = String.Empty
        txtCreatedDateMax.Text = String.Empty
        txtCreatedDateMin.Text = String.Empty
        txtCreatedDateMax.Text = String.Empty
        ddlStatusDelete.SelectedIndex = 0

    End Sub


    Private Sub LoadUpload()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        divError.Visible = False
        divSuccessful.Visible = False

        Try
            objDB.OpenConnection()

            Dim Name, Phone, Position, ICNo, CreatedDateMin, CreatedDateMax As String

            If txtName.Text <> String.Empty Then
                Name = " AND Name LIKE @Name"
                objDB.AddParameter("Name", Data.SqlDbType.NVarChar, "%" & txtName.Text.Trim & "%")
            Else
                Name = String.Empty
            End If

            If txtPhone.Text.Trim <> String.Empty Then
                Phone = " AND HandPhone LIKE @Phone"
                objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, "%" & txtPhone.Text.Trim & "%")
            Else
                Phone = String.Empty
            End If

            If txtICNo.Text <> String.Empty Then
                ICNo = " AND ICNo LIKE @ICNo"
                objDB.AddParameter("ICNo", Data.SqlDbType.NVarChar, "%" & txtICNo.Text.Trim & "%")
            Else
                ICNo = String.Empty
            End If

            If txtPosition.Text <> String.Empty Then
                Position = " AND Position LIKE @Position"
                objDB.AddParameter("Position", Data.SqlDbType.NVarChar, "%" & txtPosition.Text.Trim & "%")
            Else
                Position = String.Empty
            End If

            If txtCreatedDateMin.Text.Trim <> String.Empty Then
                CreatedDateMin = " AND CreateDate >= @CreatedDateMin"
                objDB.AddParameter("CreatedDateMin", Data.SqlDbType.Date, txtCreatedDateMin.Text.Trim)
            Else
                CreatedDateMin = String.Empty
            End If

            If txtCreatedDateMax.Text.Trim <> String.Empty Then
                CreatedDateMax = " AND CreateDate <= @CreatedDateMax"
                objDB.AddParameter("CreatedDateMax", Data.SqlDbType.Date, txtCreatedDateMax.Text.Trim)
            Else
                CreatedDateMax = String.Empty
            End If

            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, ddlStatusDelete.SelectedValue)

            objDB.dbVariable.SQLString = "SELECT CandidateID,Name,HandPhone,ICNo" & _
                                        ",Position, Gender, HomeAddress" & _
                                        ",CreateDate from Mobile_JobCandidate " & _
                                        "WHERE StatusDelete = @StatusDelete " & _
                                         Name & Phone & ICNo & Position & _
                                        CreatedDateMin & CreatedDateMax & _
                                        lblhidSortType.Text

            objDB.FillData(TempDT, "Mobile_JobCandidate", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()

            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repUpload.DataSource = objPaged
            repUpload.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount

        Catch ex As Exception
            divError.Visible = True
            lblMsgError.Text = ex.Message.ToString
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Protected Sub repUpload_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repUpload.ItemCommand
        If e.CommandName = "ViewList" Then
            'Edited by minginn - To allow user to go back to the search page with same search selection
            '1-Back Page
            '2-Page Size
            '3-Current Page

            Dim strBack As String
            strBack = "MobileJob.aspx" & "ڡ" & PageSize & "ڡ" & CurrentPage & "ڡ" & _
                    txtName.Text.Trim & "ڡ" & txtPhone.Text.Trim & "ڡ" & txtICNo.Text.Trim & "ڡ" & "ڡ" & txtPosition.Text.Trim & "ڡ" & _
                    ddlStatusDelete.SelectedValue.Trim & "ڡ" & txtCreatedDateMin.Text.Trim & "ڡ" & txtCreatedDateMax.Text.Trim & "ڡ" & ddlStatusDelete.SelectedValue.Trim

            Session.Add("Back", strBack)

        End If
        If e.CommandName.ToUpper = "DELETEITEM" Then

            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE Mobile_JobCandidate SET StatusDelete=1 WHERE CandidateId=@CandidateId"
                objDB.AddParameter("CandidateId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repUpload.Items(e.CommandArgument).FindControl("lblRepId"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Entry Deleted"
                lblMsgSuccessful.Visible = True
                divSuccessful.Visible = True
                LoadUpload()
            Catch ex As Exception
                lblMsgError.Text = "Entry Not Deleted. Error : " + ex.Message
                divError.Visible = True
            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")

            If arr(0).ToString.Contains("MobileJob.aspx") Then
                PageSize = CInt(arr(1).ToString)
                CurrentPage = CInt(arr(2).ToString)

                txtName.Text = arr(4).ToString
                txtPhone.Text = arr(5).ToString
                txtICNo.Text = arr(6).ToString
                txtPosition.Text = arr(7).ToString
                ddlStatusDelete.SelectedValue = arr(8).ToString
                txtCreatedDateMin.Text = arr(9).ToString
                txtCreatedDateMax.Text = arr(10).ToString
                Session.Remove("Back")
                LoadUpload()

            Else
                Session.Remove("Back")
            End If
        Else
            If Not Page.IsPostBack Then

                lblStaffUID.Text = Session.Item("StaffId").ToString
                lblStaffType.Text = Session.Item("StaffType").ToString
                lnkCreateDate_Click(sender, e)
            End If
        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

        End If
    End Sub
    Protected Sub lnkName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkName.Click
        If lnkName.Text = "Name▼" Then
            lnkName.Text = "Name▲"
        Else
            lnkName.Text = "Name▼"
        End If

        lblhidSortType.Text = " ORDER BY Name " & SortType(lnkName.Text) & _
                            ", CreateDate " & SortType(lnkCreateDate.Text) & _
                            ", HandPhone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPhone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPhone.Click
        If lnkPhone.Text = "Phone▼" Then
            lnkPhone.Text = "Phone▲"
        Else
            lnkPhone.Text = "Phone▼"
        End If

        lblhidSortType.Text = " ORDER BY HandPhone " & SortType(lnkPhone.Text) & _
                            ", CreateDate " & SortType(lnkCreateDate.Text) & _
                            ", Name " & SortType(lnkName.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkICNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkICNo.Click
        If lnkICNo.Text = "ICNo▼" Then
            lnkICNo.Text = "ICNo▲"
        Else
            lnkICNo.Text = "ICNo▼"
        End If

        lblhidSortType.Text = " ORDER BY ICNo " & SortType(lnkICNo.Text) & _
                            ", CreateDate " & SortType(lnkCreateDate.Text) & _
                            ", Name " & SortType(lnkName.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkCreateDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateDate.Click
        If lnkCreateDate.Text = "Creation Date▼" Then
            lnkCreateDate.Text = "Creation Date▲"
        Else
            lnkCreateDate.Text = "Creation Date▼"
        End If

        lblhidSortType.Text = " ORDER BY CreateDate " & SortType(lnkCreateDate.Text) & _
                            ", Name " & SortType(lnkName.Text) & _
                            ", HandPhone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPosition_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPosition.Click
        If lnkPosition.Text = "Position▼" Then
            lnkPosition.Text = "Position▲"
        Else
            lnkPosition.Text = "Position▼"
        End If

        lblhidSortType.Text = " ORDER BY Position " & SortType(lnkPosition.Text) & _
                            ", Name " & SortType(lnkName.Text) & _
                            ", HandPhone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkGender_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkGender.Click
        If lnkGender.Text = "Gender▼" Then
            lnkGender.Text = "Gender▲"
        Else
            lnkGender.Text = "Gender▼"
        End If

        lblhidSortType.Text = " ORDER BY Gender " & SortType(lnkGender.Text) & _
                            ", Name " & SortType(lnkName.Text) & _
                            ", HandPhone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkHomeAddress_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHomeAddress.Click
        If lnkHomeAddress.Text = "Home Address▼" Then
            lnkHomeAddress.Text = "Home Address▲"
        Else
            lnkHomeAddress.Text = "Home Address▼"
        End If

        lblhidSortType.Text = " ORDER BY HomeAddress " & SortType(lnkHomeAddress.Text) & _
                            ", Name " & SortType(lnkName.Text) & _
                            ", HandPhone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function
End Class
